//
//  GTResouseDetailViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/7/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "GTResouseDetailViewController.h"
#import "ShareResouceDetailModel.h"
#import "ShareResouceDetailTableViewCell.h"
#import "GTResouceDetailBeizhuTableViewCell.h"
#import "GTResouceDetailStatusTableViewCell.h"
@interface GTResouseDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  tableview  */
@property (strong, nonatomic) UITableView  *tableView;

/**  数据  */
@property (strong, nonatomic) NSArray  *dataSouce;
@end

@implementation GTResouseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.ID.length == 0) {
        
        [self getDataSource];
    }else{
       
        [self netWorking];
    }
    
    [self layoutUI];
}

- (void)netWorking{
   
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    __weak typeof(self) weakSelf = self;
    NSDictionary *paramer = @{@"ID": self.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpbactionResourcegetDetialYY] Parameter:paramer Success:^(id object) {
         [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.model = [ShareOpenListModel yy_modelWithDictionary:[object objectForKey:@"INFO"]];
            [weakSelf getDataSource];
        }else{
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf netWorking];
            }];

        }
        
    } Fail:^(id object) {
        [YJProgressHUD hide];
        [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
            [weakSelf netWorking];
        }];
    }];


}

- (void)action_leftButton:(UIButton *)sender{
    if (self.isModel) {
        [LPCostomPressentAnimationSimilarityPushAnimation backLastVc];
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 数据
- (void)getDataSource{
    if (!self.model || !self.model.ID) {//数据库没有改需求信息
        __weak typeof(self) weakSelf = self;
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您预约的资源已被删除" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (weakSelf.block) {
                weakSelf.block();
            }
            [weakSelf action_leftButton:weakSelf.leftButton];
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    self.titlelabel.text = self.model.NAME;
    NSString *time = [NSString stringWithFormat:@"%@ %@-%@",self.model.ORDERDATE,self.model.ORDERBEGINTIME,self.model.ORDERENDTIME];
    NSString *beizhuheight;
    if (self.model.CONTENT.length == 0) {
       beizhuheight = [NSString stringWithFormat:@"%d",60];
    }else{
       beizhuheight = [NSString stringWithFormat:@"%lf",self.model.BEIZHUHEIGHT + 60];
    }
    NSString *tuihuiheight;
    if (self.model.REASON.length == 0) {
        tuihuiheight = [NSString stringWithFormat:@"%d",60];
    }else{
        tuihuiheight = [NSString stringWithFormat:@"%lf",self.model.REASONHEIGHT + 60];
    }
    
    NSString *STATUS;
    /**  1.审核中  2、审核通过  3审核未通过  */
    if ([self.model.STATE isEqualToString:@"1"]) {
        STATUS = @"审核中";
    }else if ([self.model.STATE isEqualToString:@"2"]) {
        STATUS = @"审核通过";
    }else if ([self.model.STATE isEqualToString:@"3"]) {
        STATUS = @"审核未通过";
    }

    
    NSArray *array = @[
                       @{@"title": @"资源名称", @"content": self.model.NAME, @"cellHEIGHT": @"50"},
                       @{@"title": @"预约方", @"content": self.model.USERNAME,@"cellHEIGHT": @"50"},
                        @{@"title": @"联系方式", @"content": self.model.USERPHONE,@"cellHEIGHT": @"50"},
                       @{@"title": @"所属机构", @"content": self.model.ORG_NAME,@"cellHEIGHT": @"50"},
                       @{@"title": @"预约时间", @"content": time,@"cellHEIGHT": @"50"},
                       @{@"title": @"预约人数", @"content": self.model.NUM,@"cellHEIGHT": @"50"},
                       @{@"title": @"预约地址", @"content": self.model.ADDRESS,@"cellHEIGHT": @"50"},
                       @{@"title": @"备注", @"content": self.model.CONTENT,@"cellHEIGHT": beizhuheight},
                       @{@"title": @"预约状态",@"content": STATUS,@"cellHEIGHT": @"50"},
                       @{@"title": @"退回理由", @"content": self.model.REASON,@"cellHEIGHT": tuihuiheight}];
    NSMutableArray *kebianshuzu = [array mutableCopy];
    if (![self.model.STATE isEqualToString:@"3"]) {//审核不通过，有退回理由
        [kebianshuzu removeLastObject];
    }
    if (self.model.ORG_NAME.length == 0) {
        [kebianshuzu removeObjectAtIndex:3];
    }
    self.dataSouce = [NSArray yy_modelArrayWithClass:[ShareResouceDetailModel class] json:kebianshuzu];
    
    [self.tableView reloadData];
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
   
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShareResouceDetailModel *model = self.dataSouce[indexPath.row];
    CGFloat height = 0.0;
    if ([model.cellHEIGHT floatValue] * [UIScreen zj_screenHScale] < model.allHeight) {
        height = model.allHeight;
    }else{
        height = [model.cellHEIGHT floatValue] * [UIScreen zj_screenHScale];
    }
    return height;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ShareResouceDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.title isEqualToString:@"备注"] ||[model.title isEqualToString:@"退回理由"]) {
        GTResouceDetailBeizhuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[GTResouceDetailBeizhuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else if ([model.title isEqualToString:@"预约状态"]){
        GTResouceDetailStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[GTResouceDetailStatusTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;

    }
    else{
        ShareResouceDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ShareResouceDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        cell.row = indexPath.row;
        return cell;
    }
   
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}

@end
